#include "projectcontextmenu.h"
#include "ui_projectcontextmenu.h"

ProjectContextMenu::ProjectContextMenu(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::ProjectContextMenu)
{
    m_ui->setupUi(this);    
    setVisible(false);
    m_ui->renameLineEdit->hide();
    setWindowFlags(Qt::Popup);
    m_folderIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/folder-open-document.png")), QIcon::Normal, QIcon::Off);
    m_applicationIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/asterisk.png")), QIcon::Normal, QIcon::Off);
    m_headerIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document-attribute-h.png")), QIcon::Normal, QIcon::Off);
    m_sourceIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document-attribute-c.png")), QIcon::Normal, QIcon::Off);
    m_undefinedIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document.png")), QIcon::Normal, QIcon::Off);
}

ProjectContextMenu::~ProjectContextMenu()
{
    delete m_ui;
}

void ProjectContextMenu::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ProjectContextMenu::setObjectName(const QString& name)
{
    this->m_ui->objectName->setText(name);
}

void ProjectContextMenu::setObjectType(const QString& type)
{
    this->m_ui->objectType->setText(type);
}

void ProjectContextMenu::setObjectDescription(const QString& description)
{
    this->m_ui->descriptionTextEdit->setPlainText(description);
}

void ProjectContextMenu::setProjectSettingVisible(bool set)
{
    this->m_ui->projectSettings->setVisible(set);
}

void ProjectContextMenu::setObject(QTreeWidgetItem* object)
{
    m_object = object;
    this->setObjectName(object->text(0));
    this->setObjectType(object->text(2));
    this->setObjectDescription(object->text(3));
    if(object->parent()==0)
    {
        this->setProjectSettingVisible(true);
        this->setFileRename(false);
    }
    else
    {
        this->setProjectSettingVisible(false);
        this->setFileRename(true);
    }
}

void ProjectContextMenu::on_save_clicked()
{
    m_object->setText(3, this->m_ui->descriptionTextEdit->toPlainText());
}

void ProjectContextMenu::on_renameBtn_clicked()
{
    m_ui->renameLineEdit->show();
    m_ui->renameLineEdit->setText(this->m_ui->objectName->text());
    m_ui->renameLineEdit->setFocus();
}

void ProjectContextMenu::on_renameLineEdit_returnPressed()
{
    m_ui->objectName->setText(m_ui->renameLineEdit->text());
    m_object->setText(0, m_ui->renameLineEdit->text());
    if(this->m_ui->objectType->text().compare("Project") != 0)
    {
        QFile file(this->objectPath()+this->objectName());
        file.open(QIODevice::WriteOnly);
        switch(type(m_ui->renameLineEdit->text()))
        {
            case OType::UNDEFINED:
            {
                m_object->setIcon(0, m_undefinedIcon);
                this->setObjectType("Undefined");
                if(!file.rename(this->objectPath()+this->m_ui->renameLineEdit->text()))
                    QMessageBox::critical(this, "Can`t rename this file", file.errorString());
            }
            break;
            case OType::FOLDER:
            {
                m_object->setIcon(0, m_folderIcon);
                this->setObjectType("Folder");
            }
            break;
            case OType::HEADER:
            {
                m_object->setIcon(0, m_headerIcon);
                if(!file.rename(this->objectPath()+this->m_ui->renameLineEdit->text()))
                    QMessageBox::critical(this, "Can`t rename this file", file.errorString());
                this->setObjectType("Header");
            }
            break;
            case OType::SOURCE:
            {
                m_object->setIcon(0, m_sourceIcon);
                this->setObjectType("Source");

                if(!file.rename(this->objectPath()+this->m_ui->renameLineEdit->text()))
                    QMessageBox::critical(this, "Can`t rename this file", file.errorString());
            }
            break;
            default:
            {
                m_object->setIcon(0, m_undefinedIcon);
                this->setObjectType("Undefined");
                if(!QFile::rename(this->objectPath()+this->m_object->text(0), this->objectPath()+this->m_ui->renameLineEdit->text()))
                    QMessageBox::critical(this, "Can`t rename this file", file.errorString());
            }
        }
    }
    m_ui->renameLineEdit->hide();
}

void ProjectContextMenu::on_renameLineEdit_lostFocus()
{
    m_ui->renameLineEdit->hide();
}

void ProjectContextMenu::on_addFile_clicked()
{
    QStringList info;
    QDate date;;
    QTime time;
    info<<"Untitled"<<m_object->text(1)<<"Unknown"
            <<(QString("File created at %2 %1").arg(
                    date.currentDate().toString(Qt::TextDate),
                    time.currentTime().toString(Qt::TextDate)));
    QTreeWidgetItem *child;
    if(m_object->childCount()==0)
        child = new QTreeWidgetItem(m_object->parent(),info);
    else
        child = new QTreeWidgetItem(m_object,info);
    child->setIcon(0,m_undefinedIcon);
    this->setObject(child);
    this->on_renameBtn_clicked();
}

OType::ObjectType ProjectContextMenu::type(const QString& name)
{
    QFileInfo nfo(name);
    if(nfo.suffix().compare("h") == 0)
    {
        return OType::HEADER;
    }
    else if(nfo.suffix().compare("cpp") == 0 || nfo.suffix().compare("c") == 0)
    {
        return OType::SOURCE;
    }
    else if(nfo.suffix().compare("exe") == 0)
    {
        return OType::APPLICATION;
    }
    else if(nfo.isDir())
    {
        return OType::FOLDER;
    }
    else
    {
        return OType::UNDEFINED;
    }
}

void ProjectContextMenu::setFileRename(bool visible)
{
    this->m_ui->renameBtn->setVisible(visible);
}

void ProjectContextMenu::on_deleteBtn_clicked()
{
    QMessageBox::question(this,QString("Delete?"), QString("Delete %1?").arg(this->m_object->text(0)));
}

void ProjectContextMenu::on_projectSettings_clicked()
{

}

void ProjectContextMenu::on_saveBtn_clicked()
{

}
